package com.small.nacos.naming.core;

import java.util.*;

/**
 * @Author zhoujin
 * @Date 2022/10/13 19:28
 */
public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster{

    private Set<Instance> persistentInstances = new HashSet<>();

    private Set<Instance> ephemeralInstances = new HashSet<>();

    private Service service;

    public Cluster(String clusterName, Service service) {
        super(clusterName);
        this.service = service;
    }

    public List<Instance> allIPs() {
        List<Instance> allInstances = new ArrayList<>();
        allInstances.addAll(persistentInstances);
        allInstances.addAll(ephemeralInstances);
        return allInstances;
    }

    public List<Instance> getIPs(boolean ephemeral) {
        List<Instance> instances = new ArrayList<>();
        Set<Instance> toUpdateInstances;
        if (ephemeral){
            toUpdateInstances = ephemeralInstances;
        }else {
            toUpdateInstances = persistentInstances;
        }
        instances.addAll(toUpdateInstances);
        return instances;
    }

    public void updateIps(List<Instance> ips, boolean ephemeral) {

        Set<Instance> toUpdateInstances = new HashSet<>();

        toUpdateInstances.addAll(ips);

        if (ephemeral){
            ephemeralInstances = toUpdateInstances;
        }else {
            persistentInstances = toUpdateInstances;
        }
    }

    public Set<Instance> getPersistentInstances() {
        return persistentInstances;
    }

    public void setPersistentInstances(Set<Instance> persistentInstances) {
        this.persistentInstances = persistentInstances;
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public Set<Instance> getEphemeralInstances() {
        return ephemeralInstances;
    }

    public void setEphemeralInstances(Set<Instance> ephemeralInstances) {
        this.ephemeralInstances = ephemeralInstances;
    }
}
